<?php
	include ('../defines.php');

	FunctionsHelper::includeFiles(
		array(
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'users.php'
		)
	);

	$users = new Users();
	if (!$users->isLoggedUser())
		header('Location:' . ADMINISTRATOR_VIEW_PATH . 'login.php');

	$userLogged = $users->getLoggedUser();
	if (!empty($_POST)){
		$action = $_POST['action'];
		unset($_POST['action']);
		unset($_POST['sbmSubmit']);
		if ($action == 'm')
			$operation = $users->updateUser($_POST);
		else 
			$operation = $users->createUser($_POST);
		 
		if ($operation && (!isset($_SESSION['UserAndPasswordDontMatch']) || empty($_SESSION['UserAndPasswordDontMatch'])))
			header('Location:' . ADMINISTRATOR_VIEW_PATH . 'users.php');
	}
	if (isset($_GET['id'])){
		$userSelected = $users->getUser(
			array(
				'user_id' => $_GET['id']
			)
		);
	}else{
		$userSelected = $users->getUser();
	}

	FunctionsHelper::includeTop();
?>
<div class="withBorder" id="main">
	<script type="text/javascript">
		$(function(){			
			$('#back').click(function(){
				document.location.href='<?=ADMINISTRATOR_VIEW_PATH;?>users.php';
			});
		});
	</script>
	<div class="mainABM">
		<div class="ABMtitle"><h1>ADMINISTRADOR DE USUARIOS</h1></div>
		<?php
		$error = FunctionsHelper::getErrorInSession('UserAndPasswordDontMatch'); 
		if ($error){
		?>
		<div class="error">Las contrase&ntilde;as no coinciden.</div>
		<?php 
		}
		?>
		<form id='frmUser' class='form' method='POST'>
			<input type='hidden' name='user_id' id='user_id' value='<?= $userSelected->getUserId(); ?>' />
			<input type='hidden' name='action' id='action' value='<?= $_GET['action']; ?>' />
			<div class="divContainerRequired">
				<div class="label noLeftPadding">Usuario*: </div><input type='text' value='<?= utf8_encode($userSelected->getUsername()); ?>' class='required tinyInput' name='username' id='username' />
			</div>
			<br />
			<div class="divContainerRequired">
				<div class="label noLeftPadding">Nombre*: </div><input type='text' value='<?= utf8_encode($userSelected->getName()); ?>' class='required tinyInput' name='name' id='name' />
			</div>
			<br />
			<div class="divContainerRequired">
				<div class="label noLeftPadding">Apellido*: </div><input type='text' value='<?= utf8_encode($userSelected->getSurname()); ?>' class='required tinyInput' name='surname' id='surname' />
			</div>
			<?php 
			if ($userLogged->getUserId() == $userSelected->getUserId() || $_GET['action'] != 'm'){
			?>
			<br />
			<div class="label">Contrase&ntilde;a: </div><input type="password" id="password" name="password" class="tinyInput <?= $_GET['action'] == 'a' ? 'required' : ''; ?>" />
			<br />
			<div class="label">Confirmar contrase&ntilde;a: </div><input type="password" id="confirmationPassword" name="confirmationPassword" class="tinyInput <?= $_GET['action'] == 'a' ? 'required' : ''; ?>" />
			<?php 
			}
			if ($userLogged->getUserId() != $userSelected->getUserId()){
			?>
			<br />
			<br />
			<input type="checkbox" name='active' id='active' value='1' <? echo $userSelected->isActive() == 1 ? 'checked = "checked"' : ''; ?> /><label for="active">Activo</label>
			<?php 
			}
			?>
			<br />
			<div id="button"><input type="submit" name='sbmSubmit' id='sbmSubmit' value='Aceptar' /></div>
		</form>
		<img class="back" src='<?= ADMINISTRATOR_IMAGE_PATH . 'back.png'; ?>' id="back" width='64' height='52'/>
	</div>
</div>

<?php
	
	FunctionsHelper::includeBottom();
?>